package org.adream.account.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.RealNameTmpDao;
import org.adream.account.entity.RealNameTmpEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class RealNameTmpService {
	
	private final static Logger logger = LoggerFactory.getLogger(RealNameTmpService.class);
	
	@Autowired
	private RealNameTmpDao tmpDao;
	
	@Autowired
	private HttpSession session;

	/**
	 * 增
	 * @param RealNameTmp
	 */
	public ResultModel<String> addRealNameTmp(RealNameTmpEntity realNameTmp) {
		realNameTmp.setCreator((String) session.getAttribute("uid"));
		realNameTmp.setCts(new Date());
		realNameTmp.setModifier((String) session.getAttribute("uid"));
		realNameTmp.setMts(new Date());
		realNameTmp.setDr(Constant.ONE);
		int result = tmpDao.addRealNameTmp(realNameTmp);
		if(result == 0) {
			logger.warn("新增认证realNameTmp失败,uid:{}", realNameTmp.getUid());
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
		
		//此功能让在自动处理身份认证  --------改
	/*	//是否存在有效的"实名认证" 
		RealNameAuthEntity realNameAuth = authDao.queryAuthByUidAndDr(realNameTmp.getUid(), Constant.ONE);
		//若未进行过"实名认证","实名认证信息"使用"身份认证实名信息"
		if(realNameAuth == null) {
			realNameAuth = new RealNameAuthEntity();
			BeanUtils.copyProperties(realNameTmp, realNameAuth);
			//"实名认证"认证状态为"认证中"
			realNameAuth.setState(Constant.ONE);
			realNameAuth.setCheckResult("正在审核中,请耐心等候");
			int result = authDao.replaceRealNameAuth(realNameAuth);
			if(result == Constant.ZERO) {
				logger.warn("replace实名认证失败:(");
				throw new ADreamServiceException("新增失败,内部错误");
			}
		}*/
	}
	
	/**
	 * 删 逻辑删
	 * @param uid user id
	 */
	public ResultModel<String> deleteRealNameTmp(String uid) {
		RealNameTmpEntity RealNameTmp = tmpDao.queryTmpByUidAndDr(uid, Constant.ONE);
		if(RealNameTmp == null) {
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
			
		}
		RealNameTmp.setModifier((String) session.getAttribute("uid"));
		RealNameTmp.setMts(new Date());
		RealNameTmp.setDr(Constant.ZERO);
		int result = tmpDao.deleteRealNameTmp(RealNameTmp);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 改
	 * @param RealNameTmpEntity
	 */
	public ResultModel<String> updateRealNameTmp(RealNameTmpEntity realNameTmp) {
		if(session.getAttribute("uid") != null) {
			realNameTmp.setModifier((String) session.getAttribute("uid"));
		} 
		realNameTmp.setMts(new Date());
		realNameTmp.setDr(Constant.ONE);
		int result = tmpDao.updateRealNameTmp(realNameTmp);
		if(result == 0) {
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询身份认证-实名信息
	 * @param dr	删除标志
	 * @return
	 */
	public List<RealNameTmpEntity> queryRealNameTmpsByDr(Integer dr) {
		return tmpDao.queryRealNameTmpsByDr(dr);
	}
	
	/**
	 * 根据uid和删除标志查询实名认证
	 * @param uid user id
	 * @param dr  删除标志
	 * @return
	 */
	public RealNameTmpEntity queryTmpByUidAndDr(String uid,Integer dr) {
		return tmpDao.queryTmpByUidAndDr(uid, dr);
	}
	
	/**
	 * 根据uid查询身份认证-实名信息
	 * @param uid user id
	 * @return
	 */
	public RealNameTmpEntity queryTmpByUid(String uid) {
		return tmpDao.queryTmpByUid(uid);
	}
}